package com.bdqn.hospital.controller;

import com.bdqn.hospital.pojo.ChargeRecords;
import com.bdqn.hospital.pojo.Prescription;
import com.bdqn.hospital.service.ChargeRecordsService;
import com.bdqn.hospital.service.PrescriptionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @version : 1.0
 * @Author :guogd
 * @createTime: 2023/03/01 14:37
 * @Description: com.bdqn.hospital.controller
 */
@Controller
@RequestMapping("prescription")
public class PrescriptionController {
    /**
     * 药方Controller
     */

    @Autowired
    private PrescriptionService prescriptionService;

    @Autowired
    private ChargeRecordsService chargeRecordsService;
    //跳转药方收费列表页面
    @RequestMapping("goToPayList")
    public String goToPayList(Model model,@RequestParam(defaultValue = "1",required = false)Integer curPage){
        List<Prescription> allPrescriptionPay = prescriptionService.getAllPrescriptionPay();
        PageHelper.startPage(curPage,6);
        PageInfo<Prescription> pageInfo=new PageInfo<>(allPrescriptionPay);
        model.addAttribute("prescriptionList",pageInfo);
        return "payList";
    }
    //根据id获取具体药方费用，并跳转到收银payDetail页面
    @RequestMapping("goToPayDetail")
    public String goToPayDetail(Model model,Integer id){
        Prescription prescription = prescriptionService.getByPrescriptionId(id);
        System.out.println(prescription);
        model.addAttribute("prescription",prescription);
        return "payDetail";
    }

    //药方缴费
    @RequestMapping("payPrescription")
    public void payPrescription(Integer id, Integer paymentMethod, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        //修改支付状态和时间
        Integer row1 = prescriptionService.updatePayStatus(id);
        Prescription pre = prescriptionService.getByPrescriptionId(id);
        ChargeRecords chargeRecords=new ChargeRecords(null,2,pre.getPatientId(),1,paymentMethod,pre.getFee(),pre.getPayFee(),pre.getMedicareFee(),null,null,null,null);
        //新增收费记录
        Integer row2 = chargeRecordsService.addRecord(chargeRecords);
        if(row1>0&&row2>0){
            response.getWriter().print("<script>alert('缴费成功！');location.href='/prescription/goToPayList';</script>");
        }
    }

    /**
     * 获取所有的药方(可根据挂号单号)
     * @return
     */
    @RequestMapping("toDeveloperAppinfomodify")
    public String toDeveloperAppinfomodify(Model model,Integer registryId,@RequestParam(defaultValue = "1",required = false)Integer number) {
        Integer pageSize = 5;
        Integer curPage = 1;
        if(number!=null){
            curPage = number;
        }
        PageInfo<Prescription> prescriptionPageInfo = prescriptionService.getPrescriptionAll(registryId, curPage, pageSize);
        model.addAttribute("prescriptionPageInfo",prescriptionPageInfo);
        model.addAttribute("registryId",registryId);
        return "developerAppinfomodify";
    }

    /**
     * 根据药方id修改是否取药状态
     * @param prescriptionId
     * @return
     */
    @RequestMapping("sureIsFetchMedicine")
    public String sureIsFetchMedicine(Integer prescriptionId) {
        prescriptionService.updateIsFetchMedicine(prescriptionId);
        return "developerAppinfomodify";
    }

}
